package com.example.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.bean.Film;
import com.example.doubanfilm.R;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewAdapterForSearch extends RecyclerView.Adapter<HolderForSearch> {
    private Context context;

    public RecyclerViewAdapterForSearch(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public HolderForSearch onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.searchitem, parent, false);
        return new HolderForSearch(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HolderForSearch holder, int position) {
        Glide.with(context).load(getList().get(position)).into(holder.getFilmCover());
        holder.getFilmName().setText(context.getResources().getStringArray(R.array.film_name)[position]);

        // 设置评分
        String[] score = context.getResources().getStringArray(R.array.film_score_array);
        String scoreText = context.getString(R.string.film_score);
        holder.getFilmScore().setText(String.format(scoreText,score[position]));


        String[] year = context.getResources().getStringArray(R.array.film_year_array);
        String yearText = context.getString(R.string.film_year);
        holder.getFilmYear().setText(String.format(yearText,year[position]));
    }

    @Override
    public int getItemCount() {
        return getList().size();
    }

    private List<Integer> getList(){
        List<Integer> list = new ArrayList<>();

        list.add(R.drawable.filmcover1);
        list.add(R.drawable.filmcover2);
        return list;
    }
}

class HolderForSearch extends RecyclerView.ViewHolder{
    private ImageView filmCover;
    private TextView filmName;
    private TextView filmScore;
    private TextView filmYear;

    public HolderForSearch(@NonNull View itemView) {
        super(itemView);

        filmCover = itemView.findViewById(R.id.filmCover);
        filmName = itemView.findViewById(R.id.filmName);
        filmScore = itemView.findViewById(R.id.filmScore);
        filmYear = itemView.findViewById(R.id.filmYear);
    }

    public ImageView getFilmCover() {
        return filmCover;
    }

    public TextView getFilmName() {
        return filmName;
    }

    public TextView getFilmScore() {
        return filmScore;
    }

    public TextView getFilmYear() {
        return filmYear;
    }

}
